/**
* @classDescription 微信支付成功消息推送来的xml解析工具
* @author laiwp
* @createDate 2016-06-11
* @version 0.1.2
* 微信返回数据格式如下:
<xml><appid><![CDATA[wxf975507c0382a092]]></appid>\n<attach><![CDATA[111222356]]></attach>\n
<bank_type><![CDATA[CFT]]></bank_type>\n<cash_fee><![CDATA[1]]></cash_fee>\n<fee_type><![CDATA[CNY]]></fee_type>\n
<is_subscribe><![CDATA[Y]]></is_subscribe>\n<mch_id><![CDATA[10035161]]></mch_id>\n<nonce_str><![CDATA[1234567890]]>
</nonce_str>\n<openid><![CDATA[oKoMds-tLI94z7a4NRBt8bNg_z9Y]]></openid>\n<out_trade_no><![CDATA[20150705000115]]>
</out_trade_no>\n<result_code><![CDATA[SUCCESS]]></result_code>\n<return_code><![CDATA[SUCCESS]]></return_code>\n
<sign><![CDATA[B1A20748683D4856F08C702962C37449]]></sign>\n<time_end><![CDATA[20150705173309]]></time_end>\n
<total_fee>1</total_fee>\n<trade_type><![CDATA[JSAPI]]></trade_type>\n<transaction_id>
<![CDATA[1001800985201507050353228858]]></transaction_id>\n</xml>

*/

var sax = require("sax");
var parser = sax.parser(strict, {
    lowercasetags: true,
    trim: true
}),
strict = true;

var dt = new Array();
var dts = new Array();
var st = '';
//var ToUserName, FromUserName, CreateTime, MsgType, Content, MsgId;
var wxMsg;

exports.pars = function(xmlcode, cb) {
    parser.write(xmlcode).close();
    //cb(ToUserName, FromUserName, CreateTime, MsgType, Content, MsgId);
    cb(wxMsg);
};
parser.onerror = function(e) {
    // an error happened.
    console.log('error');
};
parser.ontext = function(t) {
    // if(nm == 'billingtype' || nm == 'introduction')
    // st += '"' + nm +'":"' + t +'"';
    // else st += '"' + nm +'":"' + t +'",';
    st += '"' + nm + '":"' + t + '",';
};
parser.onready = function(a) {
    //console.log('aaaaaaaaaa:' + nm+"----> "+t);	
};

parser.onopentag = function(tag) {
    nm = tag.name;
    //console.log('xml 字段:' + nm);
    if (nm == 'xml') st = '{';
};
parser.onclosetag = function(tag) {
    if (tag == 'xml') {
        st += '}';
        //console.log('文件解析onclosetag完成!!!' + st.substring(st.length-2,st.length-1));
        if (st.substring(st.length - 2, st.length - 1) == ',') {
            var k = st.lastIndexOf(',');
            var str = st.substring(0, k);
            st = str + '}';
        }
        //if(tag == 'mercname')
        console.log('文件解析onclosetag完成0!!!' + st);
        dt.push(JSON.parse(st));
        st = '';
    };
};
parser.oncdata = function(data) {
	//sys.print("<![CDATA["+data+"]]>");
	st += '"' + nm + '":"' + data + '",';
};
parser.oncomment = function(comment) {
    console.log('文件解析oncomment完成!!!');
    //sys.print("<!--"+comment+"-->");
};
parser.onend = function() {
    console.log('文件解析onend完成!!!dt.length=' + dt.length);
    for (var i = 0; i < dt.length; i++) {
        if(dt[i].return_code == 'SUCCESS'){
            if(dt[i].result_code == 'SUCCESS'){
                wxMsg = {
                    "openid":dt[i].openid,
                    "is_subscribe":dt[i].is_subscribe,
                    "trade_type":dt[i].trade_type,
                    "bank_type":dt[i].bank_type,
                    "total_fee":dt[i].total_fee,
                    "coupon_fee":dt[i].coupon_fee,
                    "fee_type":dt[i].fee_type,
                    "transaction_id":dt[i].transaction_id,
                    "out_trade_no":dt[i].out_trade_no,
                    "attach":dt[i].attach,
                    "time_end":dt[i].time_end,
                    "result_code":dt[i].result_code,
                    "return_code":dt[i].return_code,
                    "return_msg":dt[i].return_msg
                };  
            }else{
                wxMsg = {
                    "openid":dt[i].openid,
                    "mch_id":dt[i].mch_id,
                    "device_info":dt[i].device_info,
                    "nonce_str":dt[i].nonce_str,
                    "sign":dt[i].sign,
                    "result_code":dt[i].result_code,
                    "err_code":dt[i].err_code,
                    "err_code_des":dt[i].err_code_des,
                    "return_code":dt[i].return_code,
                    "return_msg":dt[i].return_msg
                };  
            }
                
        }else{
            wxMsg = {
                "return_code":dt[i].return_code,
                "return_msg":dt[i].return_msg
            };      
        }
		
    };

};